package com.sky.controller.admin;


import com.aliyun.oss.AliOssUtil;
import com.sky.result.Result;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/admin/common")
public class UploadController {
    @Autowired
    private AliOssUtil aliOssUtil;
    @RequestMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {

        if (!file.isEmpty()){
            try {
                String originalFilename = file.getOriginalFilename();
                if (originalFilename != null && !originalFilename.isEmpty()){
                    String url = aliOssUtil.upload(file.getBytes(), originalFilename);
                    return Result.success(url);
                }else {
                    return Result.error("文件名为空");
                }
            } catch (IOException e) {
                log.error("上传失败" ,e);
                return Result.error("上传失败"+ e.getMessage());
            }
        }

        return Result.error("上传失败");
    }
}
